<?php echo $this->element('paginator_ajax_settings'); ?>
<h1>Xem tất cả khách hàng</h1>

<div id="filter">
    <div class="search">
        Tìm kiếm:
        <?php echo $this->Form->input('Customer.query', array('type' => 'text', 'value' => isset($query) ? htmlspecialchars_decode($query) : '', 'class' => 'textfield', 'label' => false, 'div' => false)); ?> <span class="light">Tên, Mã khách, ĐT</span>
    </div>

    <br class="clr" />
</div>

<script type="text/javascript">
$(document).ready(function (){
    var timerid;
    <?php if (isset($query)): ?>
    $('#CustomerQuery').focus();
    <?php endif; ?>
    $('#CustomerQuery').keyup(function (e){
        if (e.keyCode == 13)
        {
            search($('#CustomerQuery').val());
        }
    });
    $('#CustomerQuery').click(function (e){
        $('#CustomerQuery').select();
    });
    function search(query)
    {
        //query = $('#CustomerQuery').val();
        clearTimeout(timerid);
        timerid = setTimeout(function (){
            $.ajax({
                async: true,
                type: "POST",
                url: "customers/index/query:" + query,
                //data: "",
                success: function(msg){
                    $('#main').html(msg);
                },
                beforeSend: function (XMLHttpRequest){
                    $("#busy-indicator").fadeIn();
                },
                complete: function (XMLHttpRequest, textStatus){
                    $("#busy-indicator").fadeOut();
                }
            });
        }, 600);
    }
    var availableTags = [
        <?php
        if (!empty($all_customers))
        {
            $count = 0;
            $customers_count = sizeof($all_customers);
            foreach ($all_customers as $customer)
            {
                echo "\"" . $customer['Customer']['information'] . "\"" . ($count < $customers_count ? "," : "");
                $count++;
            }
        }
        ?>
    ];
    $( "#CustomerQuery" ).autocomplete({
        source: availableTags,
        select: function (event, ui){
            search(ui.item.value.trim());
        }
    });
});
</script>

<table class="table1">
    <tr>
        <th><?php echo $this->Paginator->sort('Mã khách', 'code'); ?></th>
        <th><?php echo $this->Paginator->sort('Tên khách', 'first_name'); ?></th>
        <th><?php echo $this->Paginator->sort('ĐT', 'telephone'); ?></th>
        <th><?php echo $this->Paginator->sort('Email', 'email'); ?></th>
        <th><?php echo $this->Paginator->sort('Địa chỉ', 'address'); ?></th>
        <th><?php echo $this->Paginator->sort('Đêm phòng', 'total_days'); ?></th>
        <th><?php echo $this->Paginator->sort('Tiền phòng', 'total_spent'); ?></th>
        <th></th>
    </tr>
    <?php foreach ($customers as $customer): ?>
    <tr>
        <td><?php echo $customer['Customer']['code']; ?></td>
        <td><?php echo $customer['Customer']['full_name']; ?></td>
        <td><?php echo $customer['Customer']['telephone']; ?></td>
        <td><?php echo $customer['Customer']['email']; ?></td>
        <td><?php echo $customer['Customer']['address']; ?></td>
        <td align="right"><?php echo $customer['Customer']['total_days'] ?></td>
        <td align="right"><?php echo number_format($customer['Customer']['total_spent'], 1, '.', ',') ?></td>
        <td>
            <?php echo $this->Html->link('Xem', array('controller' => 'customers', 'action' => 'show', $customer['Customer']['id'])); ?>
            | <?php echo $this->Html->link('Sửa', array('controller' => 'customers', 'action' => 'edit', $customer['Customer']['id'])); ?>
            <?php if (empty($hide)): ?>
            | <?php echo $this->Html->link('Xóa', array('controller' => 'customers', 'action' => 'delete', $customer['Customer']['id']), array('class' => $hide ? $hide : ''), "Bạn có chắc xóa khách hàng này?"); ?>
            <?php endif; ?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>

<?php echo $this->element('paginator'); ?>